package com.hzdesk.media.koreabatchmatvector.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

import com.orhanobut.logger.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

/**
 * Created by Administrator on 2018/6/27.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class DeleteDirTest {
    private static String path = "file:///android_asset/advertising/";
    public static boolean deleteDir(String path){
        File file = new File(path);
        if(!file.exists()){//判断是否待删除目录是否存在
            System.err.println("The dir are not exists!");
            return false;
        }
        String[] content = file.list();//取得当前目录下所有文件和文件夹
        for(String name : content){
            File temp = new File(path, name);
            if(temp.isDirectory()){//判断是否是目录
                deleteDir(temp.getAbsolutePath());//递归调用，删除目录里的内容
                temp.delete();//删除空目录
            }else{
                if(!temp.delete()){//直接删除文件
                    System.err.println("Failed to delete " + name);
                }
            }
        }
        return true;
    }

    public static void copyAssetsSD(Context mContext){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String str = Environment.getExternalStorageDirectory().getAbsolutePath();
            File f = new File(str);
            File[] files = f.listFiles();
            boolean isflage = isContainsFile(files,"AdvertInfo");
            File file = new File(f, "AdvertInfo");
            if (!isflage) {  // todo AdvertInfo 文件不存在先创建
                file.mkdirs();
                Logger.d("initDate %s", file.getAbsolutePath());
            }else{
               DeleteDirTest.deleteDir(file.getAbsolutePath());
            }
            try {
                    String[] fileName =  mContext.getResources().getAssets().list("advertising");
                    for (String s : fileName) {
                        copyFileUsingApacheCommonsIO(s,file.getAbsolutePath()+"/"+s,mContext);
                        //FileUtil.copyFile(new File(path+s),file.getAbsolutePath()+"/"+s);
                    }
            } catch (Exception e) {
                Log.e("SplashPersenter", "normalDate  11111111: "+e );
            }
        }
    }

    private static void copyFileUsingApacheCommonsIO(String source, String dest,Context context) throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = context.getClass().getClassLoader().getResourceAsStream("assets/aa/cc.jpg");
            output = new FileOutputStream(new File(dest));
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        }catch (Exception e){
            Log.e("SplashPersenter", "copyFileUsingApacheCommonsIO: "+e );
        }finally {
            input.close();
            output.close();
        }
    }



    public static  boolean  isContainsFile(File[] files,String filePath) {
        boolean flage = false;
        for (File file : files) {
            if(file.getName().equals(filePath)){
                flage = true;
            }
        }
        return flage;
    }
    // 判断sdCard挂载 已挂载就创建 AdvertInfo文件夹
    public static boolean crteateSdCrad(String path){
        boolean flage = false;
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            String str = Environment.getExternalStorageDirectory().getAbsolutePath();
            File f = new File(str);
            File[] files = f.listFiles();
            boolean isflage = isContainsFile(files,path);
            File file = new File(f, path);  //AdvertInfo
            if (!isflage) {  // todo AdvertInfo 文件不存在先创建
                file.mkdirs();
               // Log.e("initDate ============ ", file.getAbsolutePath());
            }
            flage = true;
            return flage;
        }
        return false;
    }
    // 获取 广告存放的路径
    public static String getPath(String path){
        String str = Environment.getExternalStorageDirectory().getAbsolutePath();
        File f = new File(str);
        File file =  new File(f,path);
        if(!file.exists()){
            file.mkdirs();
        }
        return file.getAbsolutePath();  //AdvertInfo
    }
    // 获取下载的广告文件名称
    public static String getDownloadFileName(String path){
        String filename = null;
        try {
            String[] arr = path.split("/");
            filename = arr[arr.length-1];
        } catch (Exception e) {
        }
        return filename;
    }
    // 清空
    public static String clear(){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            String str = Environment.getExternalStorageDirectory().getAbsolutePath();
            String path = str +"/AdvertInfo";
           File file =  new File(path);
           if(!file.exists()){
               file.mkdirs();
           }
            deleteDir(file.getAbsolutePath());
           return file.getAbsolutePath();
        }
        return "";
    }

}
